package com.foton.limiter.construstlimiter.base.config;

import com.foton.limiter.construstlimiter.base.validator.ParamValidator;
import com.foton.limiter.construstlimiter.base.validator.TransferRequest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author RenPu
 * @Date 2023/9/4 15:40
 * @Version 1.0
 * @Description: 参数校验配置类
 **/
@Service
public class ValidatorManager implements InitializingBean {

    @Autowired
    private ApplicationContext applicationContext;

    private List<ParamValidator> container;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, ParamValidator> beans = applicationContext.getBeansOfType(ParamValidator.class);
        container=new CopyOnWriteArrayList<>(beans.values());
    }


    /**
     * 校验参数方法
     * @param transferRequest
     */
    public void checkParam(TransferRequest transferRequest){
        for (ParamValidator paramValidator : container) {
            paramValidator.checkParam(transferRequest);
        }
    }
}
